package com.xxxx.protal.controller;

import com.xxxx.protal.pojo.Goods;
import com.xxxx.protal.pojo.MyResult;
import com.xxxx.protal.pojo.Search;
import com.xxxx.protal.serveice.EsSearchService;
import com.xxxx.rpc.service.SearchService;
import com.xxxx.rpc.service.impl.SearchServiceImpl;
import com.xxxx.rpc.vo.GoodsVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lwf
 * @title: GoogsSearch
 * @projectName shop
 * @description: TODO
 * @date 2021/1/510:08
 */
@Controller
@RequestMapping("/search")
public class GoogsSearch {
    @Resource
    private EsSearchService service;
    @PostMapping("/searchGoods")
    @ResponseBody
    public MyResult searchGoods(Search search){
        MyResult result=new MyResult();
        List<Goods> goodsList = service.pageSearch(search.getSearchStr(), search.getPageNum(), search.getPageSize());
        result.setPageNum(search.getPageNum());
        //有下页
        if(goodsList.size()>0&&null!=goodsList.get(0).getGoodsNum()){
            result.setTotal((int) Math.ceil((goodsList.get(0).getGoodsNum()/search.getPageSize())*1.0));
            result.setHasNext((search.getPageNum()*search.getPageSize())<goodsList.get(0).getGoodsNum());
        }
        //有前页
        result.setHasPre(search.getPageNum()>1);
        result.setResult(goodsList);
        return result;
    }
    //index?searchStr=手机&x=10&y=9
    @GetMapping("/index")
    public String index(String searchStr, Integer x, Integer y, Model model) throws InterruptedException {
        System.out.println("搜索");
        System.out.println(searchStr+"----"+x+","+y);
        List<Goods> vos = service.pageSearch(searchStr, 1, 10);
        model.addAttribute("goodsList",vos);
        model.addAttribute("searchStr",searchStr);
        return "/search/doSearch";
    }
}
